package com.changpay.zywy.ui.activity.serivce

import android.content.Intent
import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.changpay.zywy.R
import com.changpay.zywy.adapter.SelectSnAdapter
import com.changpay.zywy.app.App.Companion.context
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.bean.MyMachineToolsList
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.wight.SpaceItemDecoration
import kotlinx.android.synthetic.main.activity_sn_select_recycle.*
import kotlinx.android.synthetic.main.title_layout.*
import java.util.*

class SelectSnCodeActivity : BaseActivity(), IView {

    var others_member_id = ""
    var sn_show = ""
    var machine_type = ""
    var goods_id = ""
    var type = ""

    val sn_code: StringBuilder = StringBuilder()

    override fun showLoading() {
    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)
    }

    override fun showMsg(msg: String) {
        showToast(msg)
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }

    override fun attachLayoutRes(): Int {
        return R.layout.activity_sn_select_recycle
    }

    override fun initData() {
    }


    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.iv_back -> finish()
            R.id.tv_canal -> finish()
            R.id.tv_reset -> {
                datas.forEach { it.isSelect = false }
                mAdapter.notifyDataSetChanged()
            }
            R.id.tv_submit -> {
                var number = 0
                datas.forEach {
                    if (it.isSelect) {
                        number++
                        sn_code.append(it.sn_code).append(",")
                    }
                }
                if (sn_code.isEmpty()) {
                    showToast("至少选择一个SN号")
                    return@OnClickListener
                }
                sn_code.deleteCharAt(sn_code.length - 1)
                val split = sn_code.toString().split(",")
                if (split.size == 1) {
                    sn_show = sn_code.toString()
                } else {
                    sn_show = split[0] + "~" + split[number - 1]
                }
                Intent().run {
                    putExtra("sn_code", sn_code.toString())
                    putExtra("sn_show", sn_show)
                    putExtra("number", number.toString())  // 数量
                    setResult(400, this)
                    finish()
                }
            }
        }
    }


    override fun initView() {
        tv_title.text = intent.getStringExtra("title")
        goods_id = intent.getStringExtra("goods_id")
        type = intent.getStringExtra("type")
        initRecyclerView()
        iv_back.setOnClickListener(onClickListener)
        tv_canal.setOnClickListener(onClickListener)
        tv_reset.setOnClickListener(onClickListener)
        tv_submit.setOnClickListener(onClickListener)
    }

    override fun start() {
        loadData()
    }

    private fun loadData() {
        // 加载底部数据
        val params = HashMap<String, String>()
        params["client"] = "android"
        params["key"] = token
        params["goods_id"] = goods_id
        if (type == "backallow") {
            params["member_id"] = intent.getStringExtra("member_id")
        }
        datas.clear()
        RetrofitHelper.service.showProductSNCode(params).sss(this) {
            it.result.data.let {
                it.forEach {
                    val temp = MyMachineToolsList.ListBean()
                    temp.sn_code = it.sn_code
                    temp.goods_id = it.id.toInt()
                    datas.add(temp)
                }

            }
            mAdapter.notifyDataSetChanged()
        }


    }


    /**
     * datas
     */
    private val datas = mutableListOf<MyMachineToolsList.ListBean>()

    /**
     * RecyclerView Divider
     */
    private val recyclerViewItemDecoration by lazy {
        this.let {
            SpaceItemDecoration(context!!)
        }
    }

    /**
     * Adapter
     */
    private val mAdapter: SelectSnAdapter by lazy {
        SelectSnAdapter(context, datas)
    }


    /**
     * LinearLayoutManager
     */
    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(context)
    }


    /**
     * ItemClickListener
     */
    private val ItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
        //        if (datas.size != 0) {
////            val data = datas[position]
////            Intent().run {
////                setResult(400, this)
////            }
////            finish()
//        }
    }


    private fun initRecyclerView() {
        recyclerView.run {
            layoutManager = linearLayoutManager
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            recyclerViewItemDecoration.let { addItemDecoration(it) }
        }
        mAdapter.run {
            bindToRecyclerView(recyclerView)
//            setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
            this.onItemClickListener = ItemClickListener
            setEmptyView(R.layout.fragment_empty_layout)
        }
    }

}